import 'dart:io';

import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:flutter_widget/custom/custom_appbar.dart';
import 'package:flutter_widget/utils/platform.dart';

import 'package:image_picker/image_picker.dart';

// ignore: must_be_immutable
class ImagePickerPage extends StatefulWidget {
  String name;
  ImagePickerPage({Key? key, required this.name}) : super(key: key);
  @override
  _ImagePickerPageState createState() => _ImagePickerPageState();
}

class _ImagePickerPageState extends State<ImagePickerPage> {
  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  List<BrnCommonActionSheetItem> actions = [
    BrnCommonActionSheetItem(
      '拍照',
      actionStyle: BrnCommonActionSheetItemStyle.normal,
    ),
    BrnCommonActionSheetItem(
      '相册',
      titleStyle:
      const TextStyle(color: Colors.red, fontWeight: FontWeight.bold),
      actionStyle: BrnCommonActionSheetItemStyle.normal,
    ),
  ];
  //用户本地图片
  late File _userImage = File(""); //存放获取到的本地路径

  final picker = ImagePicker();
  /*拍照*/
  // ignore: unused_element
  _takePhoto() async {
    final pickedFile = await picker.getImage(source: ImageSource.camera);
    if (mounted) {
      setState(
        () {
          if (pickedFile != null) {
            _userImage = File(pickedFile.path);
            print('你选择的路径是：${_userImage.toString()}');
          } else {
            // ignore: avoid_print
            print('No image selected.');
          }
        },
      );
    }
  }

  /*相册*/
  _openGallery() async {

      final pickedFile = await picker.getImage(source: ImageSource.gallery);
      if (mounted) {
        setState(
          () {
            if (pickedFile != null) {
              _userImage = File(pickedFile.path);
              print('你选择的路径是：${_userImage.toString()}');
            } else {
              // ignore: avoid_print
              print('No image selected.');
            }
          },
        );
      }

  }

  Widget build(BuildContext context) {
    return Scaffold(
      appBar: CustomAppbar(widget.name, Theme.of(context).primaryColor),
      body: Center(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            GestureDetector(
                onTap: () async {
                  if(PlatformUtils.isIOS==false && PlatformUtils.isAndroid==false) {
                    print("当前平台不支持");
                    BrnToast.show(
                      "当前平台不支持",
                      context,
                      duration: BrnDuration.short,
                    );

                    return ;
                  }
                  List? res = await showModalBottomSheet(
                      context: context,
                      backgroundColor: Colors.transparent,
                      builder: (BuildContext context) {
                        return BrnCommonActionSheet(
                          actions: actions,
                          clickCallBack: (
                            int index,
                            BrnCommonActionSheetItem actionEle,
                          ) {
                            String title = actionEle.title;
                          },
                        );
                      });
                  // ignore: avoid_print
                  if (res != null  ) {
                     if(res[0] == 0){
                       _takePhoto();
                     }
                     if(res[0] == 1){
                       _openGallery();
                     }
                  } else {
                    print(res);
                  }
                },
                child: renderWidget(_userImage))
          ],
        ),
      ),
    );
  }

  renderWidget(images) {
    if (images.path != "") {
      return ClipOval(
        child: Image.file(
          images,
          fit: BoxFit.cover,
          width: 80,
          height: 80,
        ),
      );
    }
    return Icon(
      Icons.person,
      size: 60,
    );
  }
}
